﻿// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

Shader "Custom/Flag/FlagWaveShader_Obj"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "white" {}
		_AlphaTex("Texture", 2D) = "white"{}
		_Color("Color Tint", Color) = (1,1,1,1)
		_LightFactor("Light Factor", Range(0,2)) = 1

	}
	SubShader
	{
		Tags
		{ 
			"Queue"="Transparent" 
			"IgnoreProjector"="True" 
			"RenderType"="Transparent" 
			"PreviewType"="Plane"
			"CanUseSpriteAtlas"="True"
		}
			
		Pass
		{
			ZWrite Off
			Blend SrcAlpha OneMinusSrcAlpha

			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag

			
			#include "UnityCG.cginc"
			#include "Lighting.cginc"

			struct a2v
			{
				float4 vertex : POSITION;
				float2 uv : TEXCOORD0;
				float3 normal : NORMAL;
				fixed4 color : COLOR;
			};

			struct v2f
			{
				float2 uv : TEXCOORD;
				float3 normal : TEXCOORD2;
				
				float4 vertex : SV_POSITION;

				fixed4 color : COLOR;
			};

			sampler2D _MainTex;
			float4 _MainTex_ST;
			sampler2D _AlphaTex;
			float4 _AlphaTex_ST;

			fixed4 _Color;
			float _LightFactor;

			v2f vert (a2v v)
			{
				v2f o;

				o.vertex = UnityObjectToClipPos(v.vertex);
				o.uv = TRANSFORM_TEX(v.uv, _MainTex);
				o.color = v.color;
				o.normal = v.normal;

				return o;
			}
			
			fixed4 frag (v2f i) : SV_Target
			{
				
				fixed4 mainTexCol = tex2D(_MainTex, i.uv);
				fixed4 alpha = tex2D(_AlphaTex, i.uv);

				mainTexCol.a = mainTexCol.a * alpha.r * i.color.a;

				fixed4 albedo = mainTexCol * _Color * _LightFactor;

				fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT * albedo.rgb;

				float3 worldNormal = UnityObjectToWorldNormal(i.normal);

				float3 lightDir = UnityWorldSpaceLightDir(i.vertex);

				fixed3 diffuse = _LightColor0.rgb * albedo.rgb * saturate(dot(worldNormal, lightDir));

				return fixed4(ambient + diffuse, mainTexCol.a );
			}
			ENDCG
		}
	}
}
